let $FZF_DEFAULT_OPTS = ' --reverse --info=inline --bind ctrl-b:preview-up,ctrl-f:preview-down'
au FileType fzf tnoremap <buffer> <C-j> <Down>
au FileType fzf tnoremap <buffer> <C-k> <Up>
au FileType fzf tnoremap <buffer> <C-n> <Nop>
au FileType fzf tnoremap <buffer> <C-p> <Nop>
" preview position
let g:fzf_command_prefix = 'Fzf'
" [Buffers] Jump to the existing window if possible
let g:fzf_buffers_jump = 1
" [[B]Commits] Customize the options used by 'git log':
let g:fzf_commits_log_options = '--graph --color=always --format="%C(auto)%h%d %s %C(black)%C(bold)%cr"'
" [Commands] --expect expression for directly executing the command
let g:fzf_commands_expect = 'alt-enter'
let g:fzf_action = {
            \ 'enter':  'edit',
            \ 'ctrl-t': 'tab split',
            \ 'ctrl-x': 'split',
            \ 'ctrl-]': 'vsplit'
            \ }
nmap m<tab> <plug>(fzf-maps-n)
xmap m<tab> <plug>(fzf-maps-x)
omap m<tab> <plug>(fzf-maps-o)
" layout
if has('nvim') || has('patch-8.2.191')
    let g:fzf_layout = {
                \ 'window': {'width': 0.9, 'height': 0.9, 'border': 'rounded'}
                \ }
    let g:fzf_commands_layout = {
                \ 'window': {'width': 0.4, 'height': 0.9, 'border': 'rounded'}
                \ }
    function s:fzf_updata_position()
        if &columns > &lines * 3
            let g:fzf_preview_window = ['right,45%', '?']
        else
            let g:fzf_preview_window = ['up,40%', '?']
        endif
        let g:vista_fzf_preview = g:fzf_preview_window[0:0]
    endfunction
    call s:fzf_updata_position()
    au VimResized * call s:fzf_updata_position()
else
    let g:fzf_layout = {'down': '~30%'}
    let g:fzf_commands_layout = g:fzf_layout
    let g:fzf_preview_window = ['right,45%', '?']
    let g:vista_fzf_preview = g:fzf_preview_window[0:0]
endif
" fzfggrep
command! -bang -nargs=* FzfGGrep
            \ call fzf#vim#grep(
            \    'git grep -I --line-number --color=always -- '.shellescape(empty(<q-args>) ? '^' : <q-args>),
            \     fzf#vim#with_preview(),
            \     <bang>0)
" ---------------
" fzf_commands
" ---------------
function FzfCallCommands(prompt, ...)
    let prompt = a:prompt
    if a:0 == 0
        return
    endif
    let results = []
    if a:0 > 1
        let last = a:000[-1]
        let search_commands = a:000[:-2]
        if type(last) == type([])
            let ignore_cmds = [prompt] + last
        else
            let ignore_cmds = [prompt, last]
        endif
        if len(ignore_cmds) > 1
            let ignore_reg = join(ignore_cmds, '\|')
        else
            let ignore_reg = ignore_cmds[0]
        endif
    else
        let search_commands = [a:1]
        let ignore_reg = prompt
    endif
    for command in search_commands
        let commands = split(Execute("command " . command), '\n')[1:]
        for line in commands
            for sp in split(line, '\s\+')
                if sp =~# "^" . command && sp !~# ignore_reg
                    if sp =~ '#'
                        break
                    elseif sp =~ ',' || sp =~ '\.'
                        break
                    elseif sp =~ ')' || sp =~ '('
                        break
                    elseif sp =~ ']' || sp =~ '['
                        break
                    endif
                    call add(results, sp)
                    break
                endif
            endfor
        endfor
    endfor
    if g:has_popup_floating
        let height = len(results)
        let lines = &lines
        if height > 0.95 * lines
            let height = float2nr(0.95 * lines)
        elseif height < 0.35 * &lines
            let height = float2nr(0.35 * lines)
        endif
        let l:fzf_layout = {
                    \ 'window': {'width': 50, 'height': height, 'border': 'rounded'}
                    \ }
    else
        let l:fzf_layout = {'down': '~30%'}
    endif
    call fzf#run(extend({
                \ 'source': results,
                \ 'sink': function('Execute'),
                \ 'options': printf('--ansi -x --prompt "%s>"', prompt)
                \ }, l:fzf_layout), 0)
endfunction
